let app = getApp()
const db = wx.cloud.database({env: app.globalData.CLOUD_ENV})
const DBClassify = db.collection('db_classifys')
const DBInterview = db.collection('db_topic')
const util = require('../../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'h5',
    columns: [
      'h5', 
      'java', 
      '嵌入式', 
      'php', 
      'python'
    ],
    array:[1,2],
    classifys:[],
    lists: [],
    result: []
  },
  getTypeFn(e){
    let type = e.currentTarget.dataset.type;
    if(type){
      console.log(type)
      let arr = this.data.classifys.filter(item=>{
         return item.type == type
      })
      this.setData({
        result: arr ,
        type: type
      })
    }
  },
  // 点击分类
  addClickFn(e){
    let text = e.currentTarget.dataset.text;
    this.getInterviewFn2(text)
  },
   // 获取题目分类列表
   async getInterviewFn(text) {
     wx.showLoading({
       title: '正在加载。。',
     })
    await DBInterview.where({
      text
      }).get()
      .then(
        content => {
          // console.log('1.0', content)
          let {data} = content;
          if(data.length!=0){
            let arr = data.map(item=> {
              let obj = {...item}
              obj.date =  util.formatTime(item.create_time)
              return obj;
            })
            this.setData({
              lists: arr
            })
          }
          else {
            this.setData({
              lists: []
            })
          }
        }
      )

      wx.hideLoading();
  },

  // 获取分类列表
  async getInterviewFn2(text) {
    let res = await DBInterview.where({text}).count()
    let count = res.total;
    let all = [];
    wx.showLoading({
      title: '正在加载。。',
    })
    for(let i = 0 ; i < count ; i+= 20){
      await DBInterview.where({
        text
        }).skip(i).get()
        .then(
          content => {
            // console.log('1.0', content)
            let {data} = content;
            if(data.length!=0){
              let arr = data.map(item=> {
                let obj = {...item}
                // let dt = new Date(item.create_time);
                obj.date =  util.formatTime(item.create_time)
                return obj;
              })
              all = all.concat(arr)
            }
          }
        )
    }
    this.setData({
      lists: all
    })
    wx.hideLoading();
  },

  // 分类名称列表
  async listClassifyFnxx() {
    let that = this;
    await DBClassify.get()
      .then(
        content => {
          // console.log('3.0', content)
          if (content.data) {
            that.setData({
              array: content.data
            })
          }
        }
      )
  },
   // 分类列表
   async listClassifyFn() {
    let res = await DBClassify.where({}).count()
    let count = res.total;
    let all = [];
    for(let i = 0 ; i < count ; i+= 20){
        await DBClassify.where({}).skip(i).get()
        .then(
          content => {
            // console.log('3.0', content)
            let {data} = content;
            if(data.length!=0){
              all = all.concat(data)
            }
          }
        )
    }

    let type = this.data.type
    let arr = all.filter(item => {
        // if(item.type == type){
          return item.type == type
        // }
    })
    this.setData({
      classifys: all,
      result: arr
    })
  },
  // 编辑
  editFn(e){
    let _id = e.currentTarget.dataset.id;
    wx.redirectTo({
      url: `../edit/edit?id=${_id}`,
    })
  },

  // 删除
  removeFn(e){
    let _id = e.currentTarget.dataset.id;
    let _openid = wx.getStorageSync('openid')
    if(_openid != app.globalData.OPENID) {
      wx.showToast({
        title: '您没有权限删除题目。',
        icon: 'none'
      })
      return false;
    }
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除该题目吗?',
      complete: (res) => {
        if (res.cancel) {
          
        }
        if (res.confirm) {
          DBInterview.doc(_id).remove().then(
            content=>{
                console.log('content',content)
                let {errMsg} = content;
                if(errMsg == "document.remove:ok") {
                    let arr = that.data.lists;
                    let newArr = arr.filter(item=> {
                      if(item._id != _id){
                        return item 
                      }
                    })
                    that.setData({
                      lists: newArr
                    })
                }
                else {
                  wx.showToast({
                    title: '删除失败。',
                    icon: 'none'
                  })
                }
            }
          )
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.listClassifyFn();
    // this.getInterviewFn2();
  },

  // 跳转主页
  toHome(){
    wx.switchTab({
      url: '../../../index/index',
    })
  },
   // 跳转编辑
   goOn(){
    wx.redirectTo({
      url: '../add/add'
    })
  }
})